package com.han.shadow.customerapp.fragment;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.han.shadow.customerapp.R;
import com.han.shadow.customerapp.activity.GoodsActivity;
import com.han.shadow.customerapp.adapter.MyBusinessAdapter;
import com.han.shadow.customerapp.adapter.MyOrdersAdapter;
import com.han.shadow.customerapp.bean.Business;
import com.han.shadow.customerapp.bean.Goods;
import com.han.shadow.customerapp.bean.Order;
import com.han.shadow.customerapp.http.Connector;
import com.han.shadow.customerapp.mystatic.MyScreen;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by ACE on 2016/4/20 0020.
 */
public class OrdersFragment extends Fragment{
    private BaseAdapter adapter = null;
    private List<Order> orders = null;
    private List<Business> businesses = new ArrayList<>();
    private List<Goods> goodses = new ArrayList<>();
    private ListView ordersList = null;
    private Connector conn = new Connector();

    private Context parent;
    private LayoutInflater inflater;
    private View view;

    private FragmentManager fManager;
    private FragmentTransaction fTransaction;
    private OrderInfoFragment orderInfoFragment =null;


    public OrdersFragment(Context context,LayoutInflater inflater){
        this.parent = context;
        this.inflater = inflater;

    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getOrders("33333");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_orders,null);

        ordersList = (ListView)view.findViewById(R.id.ordersList);

        ordersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Message msg = new Message();
                msg.what = 2;
                msg.arg1 = position;
                handler.sendMessage(msg);

            }
        });

        fManager = getFragmentManager();



        return view;
    }


    public void getOrders(final String cID){
        new Thread(new Runnable() {
            @Override
            public void run() {
                orders = conn.queryOrders(MyScreen.cid);
                for(Order order:orders){
                    List<Business> bl = conn.queryBusiness(order.getBid());
                    if(bl != null && bl.size()>0){
                        businesses.add(bl.get(0));
                    }
                    for(Map.Entry<String, Integer> entry: order.getGoodsIDs().entrySet()){
                        List<Goods> gl = conn.queryGoods(entry.getKey(),null);
                        if(gl != null && gl.size()>0){
                            goodses.add(gl.get(0));
                        }
                    }
                }
                Message msg = new Message();
                msg.what = 1;
                handler.sendMessage(msg);
            }
        }).start();

    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 1) {//更新订单列表
                if (orders != null) {
                    adapter = new MyOrdersAdapter(orders, businesses, goodses, inflater);
                    ordersList.setAdapter(adapter);
                }
            }
            else if(msg.what == 2){//打开订单详情

                orderInfoFragment = new OrderInfoFragment(orders.get(msg.arg1),goodses,parent);
                fTransaction = fManager.beginTransaction();
                orderInfoFragment.show(fTransaction, "订单详情");
            }
        }
    };

}
